package exhi.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.stp.core.util.WebUtil;
import com.stp.rbac.entity.CapabilityInfo;
import com.stp.rbac.entity.UserInfo;
import com.stp.rbac.manager.CapabilityMng;
import com.stp.rbac.manager.UserMng;
import com.stp.std.entity.LogInfo;
import com.stp.std.manager.LogMng;

import exhi.entity.EntpUser;
import exhi.manager.EntpMng;

@Controller
@SessionAttributes("userRbacEntp")
public class WelcomeEntpAct {

	private static final String PATH_ADMIN_INDEX = "/entp-index";
	private static final String PATH_ADMIN_CP = "/entpcp";
	private static final String PATH_ADMIN_BLANK = "/entpcp-blank";
	private static final String PATH_ADMIN_LOGIN = "/admin-login";
	private static final String PATH_ADMIN_LOGOUT = "/admin-logout";

	private static final String MOD = "系统登陆";

	@RequestMapping(PATH_ADMIN_INDEX)
	public void index() {
	}

	@RequestMapping(PATH_ADMIN_LOGIN)
	public String adminLogin(HttpServletRequest request,
			@RequestParam("userId") String userId,
			@RequestParam("userPwd") String userPwd, ModelMap model) {
		if (null == userId || userId.isEmpty() || null == userPwd
				|| userPwd.isEmpty()) {
			model.addAttribute("error", "用户名或密码不能为空！！");
		} else {
			List<UserInfo> lst = userMng.findByProperty("account", userId);
			if (null == lst || lst.size() == 0) {
				model.addAttribute("error", "用户名或密码不正确！！");
			} else {
				for (UserInfo user : lst) {
					if (user.getPwd().equalsIgnoreCase(userPwd)) {
						model.addAttribute("userRbacEntp", user);
						log(user.getUserId(), MOD, "成功登录");
						return "redirect:" + PATH_ADMIN_CP.substring(1);
					}
				}
			}
		}
		return "forward:" + PATH_ADMIN_INDEX.substring(1);
	}

	@RequestMapping(PATH_ADMIN_LOGOUT)
	public String adminLogout(ModelMap model) {
		if (null == model.get("userRbacEntp"))
			return "redirect:" + PATH_ADMIN_INDEX.substring(1);
		UserInfo user = (UserInfo) model.get("userRbacEntp");
		log(user.getUserId(), MOD, "成功退出");
		model.remove("userRbacEntp");
		return "redirect:" + PATH_ADMIN_INDEX.substring(1);
	}

	@RequestMapping(PATH_ADMIN_CP)
	public String admincp(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		if (null == model.get("userRbacEntp"))
			return "redirect:" + PATH_ADMIN_INDEX.substring(1);
		UserInfo user = (UserInfo) model.get("userRbacEntp");
		if (null == user)
			return "redirect:" + PATH_ADMIN_INDEX.substring(1);
		model.addAttribute("userName", user.getNickName());
		Collection<CapabilityInfo> lstMenu = userMng.getAllCapability(user
				.getId());
		List<CapabilityInfo> mainMenus = new ArrayList<CapabilityInfo>();
		String capName = "";
		int index = 0;
		for (CapabilityInfo info : lstMenu) {
			if (index == 0 && info.getLevel() == 1) {
				capName = info.getTitle();
				index++;
			}
			if (info.getLevel() == 2)
				mainMenus.add(info);
		}
		model.addAttribute("capName", capName);
		model.addAttribute("mainMenus", mainMenus);
		return PATH_ADMIN_CP.substring(1);
	}

	@RequestMapping(PATH_ADMIN_BLANK)
	public String adminblank(ModelMap model) {
		if (null == model.get("userRbacEntp"))
			return "redirect:" + PATH_ADMIN_INDEX.substring(1);
		UserInfo user = (UserInfo) model.get("userRbacEntp");
		EntpUser entpUser = entpMng.get(user.getUserId());
		if (null != entpUser && null!=entpUser.getUserType()) {
			if ("E".equalsIgnoreCase(entpUser.getUserType())){
				model.addAttribute("url", "1");
			}
			if ("Z".equalsIgnoreCase(entpUser.getUserType())){
				model.addAttribute("url", "2");
			}			
		}
		return PATH_ADMIN_BLANK.substring(1);
	}

	public void log(String userId, String type, String content) {
		LogInfo logInfo = new LogInfo();
		logInfo.setLogTime(new Date());
		logInfo.setIp(WebUtil.getIpAddr(request));
		logInfo.setUrl(request.getRequestURI());
		logInfo.setUserId(userId);
		logInfo.setLogGroup(type);
		logInfo.setContent(content);
		logMng.save(logInfo);
	}

	@Autowired
	private HttpServletRequest request;

	@Autowired
	@Qualifier("capabilityMng")
	private CapabilityMng capabilityMng;

	@Autowired
	@Qualifier("userMng")
	private UserMng userMng;

	@Autowired
	@Qualifier("logMng")
	private LogMng logMng;

	@Autowired
	@Qualifier("entpMng")
	protected EntpMng entpMng;

}
